#!/bin/bahs

## by CC

port=6379
src=/usr/local/src
dir=/usr/local/redis-${port}
name=redis-3.2.8

cd ${src}
tar zxvf ${name}.tar.gz

cd ${src}/${name}
echo "make"
make > /dev/null
echo $?

cd ${src}/${name}/src
echo "make install"
make PREFIX=${dir} install
echo $?

mkdir ${dir}/etc
mkdir ${dir}/var
chmod 777 ${dir}/var

tee ${dir}/etc/redis.conf <<-EOF
daemonize no
pidfile ${dir}/var/redis.pid
port ${port}
timeout 300
loglevel debug
logfile ${dir}/var/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir ${dir}/var/
appendonly no
appendfsync always
requirepass YunJing168
EOF

tee /usr/lib/systemd/system/redis-server-${port}.service <<-EOF
[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target

[Service]
Type=simple
PIDFile=${dir}/var/redis.pid
ExecStart=${dir}/bin/redis-server ${dir}/etc/redis.conf
ExecStop=${dir}/bin/redis-cli -a YunJing168 -h 127.0.0.1 -p  ${port} shutdown
Restart=always

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start redis-server-${port}.service
